import { getTrainInfo, getFault , getInfo} from '@/api/api'

const gMonitor = {
    namespaced:true,
    // state存储全局数据
    state: {
        trainData: [],
        faultData: {
            major : 0,
            secondary : 0,
            minor : 0 ,
            fault : []
        },
        trainInfo: {
            totalTrain: 0,
            onlineTrain: 0,
            faultTrain: 0,
            offlineTrain: 0,
            mileage:0,
            energy:0
        },
        params: {}
    },
    // mutations用于变更store中的数据
    // 通过mutations集中监控数据变更，利于维护
    // 不要在mutations异步函数
    mutations: {
        changeTrainData(state, trainData) {
            state.trainData = trainData;
        },
        changeTrainParam(state, param) {
            state.param = param;
        },
        changeFaultData(state, faultData) {
            state.faultData = faultData;
        },
        changeInfoData(state, trainInfo) {
            state.trainInfo = trainInfo;
        }
    },
    // 异步操作在actions中处理
    // action不能修改state，需通过mutation
    actions: {
        getTrainData({commit, dispatch}, param) {
            getTrainInfo(param).then(res => {
                    // console.log('参数为:',param);
                    // console.log('请求车辆数据成功，返回数据为:');
                    // console.log(res);
                    commit('changeTrainData', res.data.result);
                    commit('changeTrainParam', param);
                }).catch(err => {
                    console.log(err);
                });
        },
        getFaultData({commit, dispatch}, param) {
            getFault(param).then(res => {
                // console.log('参数为:',param);
                // console.log('请求故障数据成功，返回数据为:');
                // console.log(res);
                commit('changeFaultData', res.data.result);
            }).catch(err => {
                console.log(err);
            });
        },
        getInfoData({commit, dispatch}, param) {
            getInfo(param).then(res => {
                // console.log('参数为:',param);
                // console.log('请求信息数据成功，返回数据为:');
                // console.log(res);
                commit('changeInfoData', res.data.result);
            }).catch(err => {
                console.log(err);
            });
        },
    },
    modules: {
    }
}


export default gMonitor;